//
//  WinRecordViewController.m
//  YunGou
//
//  Created by macbook on 16/5/18.
//  Copyright © 2016年 macbook. All rights reserved.
//

#import "WinRecordViewController.h"
#import "PublishCommentViewController.h"
#import "WinRecordCell.h"
#import "WinRecordModel.h"
#import "NoMoreDataView.h"
#import "NullDataView.h"
#import <MJRefresh.h>

@interface WinRecordViewController ()
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong) NullDataView *nullDataView;
@end

@implementation WinRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
    
    [self loadData];
    
    // Do any additional setup after loading the view.
}

- (void)loadData{
    _dataArray = [NSMutableArray array];
    [MBProgressHUD showMessage:@"正在加载"];
    [NetWorkTool winRecordWithSuccess:^(id object) {
        [MBProgressHUD hideHUD];
        if ([object[@"code"] integerValue] == 1) {
            for (NSDictionary *dict in object[@"data"]) {
                WinRecordModel *model = [WinRecordModel modelWithDict:dict];
                [_dataArray addObject:model];
            }
        }
        if (_dataArray.count == 0) {
            [self.view addSubview:self.nullDataView];
            self.tableView.tableFooterView = nil;
        }else{
            if (_nullDataView) {
                [_nullDataView removeFromSuperview];
                _nullDataView = nil;
            }
            NoMoreDataView *footerView = [[NoMoreDataView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 44)];
            self.tableView.tableFooterView = footerView;
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WinRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.winRecordModel = _dataArray [indexPath.row];
    cell.showRecordBlock = ^(WinRecordModel *winRecordModel){
        PublishCommentViewController *publicCommentVC = SB(@"Mine", @"PublicCommentVC");
        publicCommentVC.winRecordModel = winRecordModel;
        [self.navigationController pushViewController:publicCommentVC animated:YES];
    };
    return cell;
}

#pragma -mark 懒加载
- (NullDataView *)nullDataView{
    if (!_nullDataView) {
        _nullDataView = [NullDataView nullDataView];
        _nullDataView.bounds = self.tableView.bounds;
    }
    return _nullDataView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
